$(function () {
    var init = true; //第一次加载树之后点击根节点
//    $('#_gsjg').combobox({
//        //过滤下拉列表
//        filter:function (q, row) {
//            if (q == "") {
//                return true;
//            }
//            var opts = $(this).combobox('options');
//            return row[opts.textField].indexOf(q) == 0;
//        }
//    });
    $("#parent_gsjg").combotree({
        loadFilter: function (rows) {
            return convert(rows);
        },
        //级联下级机构
        onChange:function (newValue, oldValue) {
            if (newValue != oldValue) {
                $("#_gsjg").combobox("clear");
                $("#_gsjg").combobox("reload", $.webPath() + "/organ/gsjg/childgsjg_json?gsjgId=" + newValue);
            }
        }
    });
    //加载树
    $("#west_gsjg_tree").tree({
        loadFilter: function (rows) {
            return convert(rows);
        },
        onClick:function (node) {
            var selected = $("#parent_gsjg").combotree("getValue");
            if (selected != node.id) {
                $("#parent_gsjg").combotree("setValue", node.id);
                gsjg_search();
            }
        },
        //新增修改删除数据后重新加载，并选中之前的节点
        onLoadSuccess:function (node, data) {
            var id = $("#parent_gsjg").combotree("getValue");
            if (id) {
                var select = $("#west_gsjg_tree").tree("find", id);
                $("#west_gsjg_tree").tree("select", select.target);
            }
            if (init) {
                var rootNode = $("#west_gsjg_tree").tree("getRoot");
                rootNode.target.click();
                init = false;
            }
            //给禁用的公司机构加红字样式
            $(".gsjg-tree-disabled").next().css('color','red');
        }
    });

    $("#gsjg_toolbar input[name='gsjg_query_type']").change(function () {
        gsjg_search();
    });
    $("#gsjg_new").click(function () {
        gsjg_new();
    });
    $("#gsjg_mod").click(function () {
        gsjg_modify();
    });
    $("#bmfwsz").click(function () {
        gsjg_szbmfw();
    });
    $("#ckbmfw").click(function () {
        gsjg_ckbmfw();
    });
    $("#gsjg_del").click(function () {
        gsjg_delete();
    });
    $("#gsjg_start").click(function () {
        changeGsjgState();
    });
});

var gsjgWin;
//新增公司机构
function gsjg_new() {
    var type = $("#gsjg_toolbar input[name='gsjg_query_type']:checked").val();
    var parentId = $("#parent_gsjg").combotree("getValue");
    gsjgWin =$.window({
        title:'添加公司机构',
        width:750,
        height:450,
        cache:false,
        url:$.webPath() + '/organ/gsjg/gsjg_content?type=' + type + "&gsjgId=" + parentId,
        modal:true,
        buttons:[
            {
                text:'保存',
                handler:gsjg_saveItem
            }
        ]
    });
}
//修改公司机构
function gsjg_modify() {
    var gsjgId = $("#gsjg_grid").datagrid("getSelected").id;
    if (gsjgId) {
        gsjgWin = $.window({
            title:'修改公司机构',
            width:750,
            height:450,
            cache:false,
            url:$.webPath() + '/organ/gsjg/gsjg_content?operation=update&gsjgId=' + gsjgId,
            modal:true,
            buttons:[
                {
                    text:'保存',
                    handler:gsjg_saveItem
                }
            ]
        });
    }
}

//设置编码范围
function gsjg_szbmfw(){
    var gsjgId = $("#gsjg_grid").datagrid("getSelected").id;
    if (gsjgId) {
        bmfwwin = $.window({
            title:'设置编码范围',
            width:750,
            height:450,
            cache:false,
            url:$.webPath() + '/organ/bmfw/bmfwedit?gsId=' + gsjgId,
            modal:true,
            buttons:[
                {
                    text:'保存',
                    handler:function(){
                        $("#bm-save").click();
                    }
                }
            ]
        });
    }
}
//查看编码范围
function gsjg_ckbmfw(){
    var gsjgId = $("#gsjg_grid").datagrid("getSelected").id;
    if (gsjgId) {
        ckbmfwwin = $.window({
            title:'设置编码范围',
            width:750,
            height:450,
            cache:false,
            url:$.webPath() + '/organ/bmfw/bmfw?gsId=' + gsjgId,
            modal:true,
            buttons:[]
        });
    }
}
//删除公司机构
function gsjg_delete() {
    var gsjgId = $("#gsjg_grid").datagrid("getSelected").id;
    if (gsjgId) {
        $.messager.confirm("确认", "确定删除？", function (r) {
            if (r) {
                $.ajax({
                    url:$.webPath() + "/organ/gsjg/delete_gsjg?gsjgId=" + gsjgId,
                    type:"json",
                    beforeSend:function () {
//                    $.messager.progress();
                      $("#gsjg_layout").mask();
                    },
                    success:function (data) {
//                     $.messager.progress("close");
                        $("#gsjg_layout").mask('hide');
                        if (data.statusCode == '200') {
                            $('#gsjg_grid').datagrid('reload');
                            $("#west_gsjg_tree").tree("reload");
                            $("#parent_gsjg").combotree("reload", $.webPath() + "/organ/gsjg/allgsjg_tree");
                            $.messager.show({
                                title:'提示框',
                                msg:data.message,
                                timeout:3000,
                                showType:'slide'
                            });
                        } else {
                            $.messager.alert('提示', data.message, 'info');
                        }
                    }
                });
            }
        })
    }
}
//保存
function gsjg_saveItem() {
    $(gsjgWin).mask();
    var type = $("#gsjg_type input[name='type']:checked").val();
    var selector;
    if (type == 0) {
        selector = "#nbbmForm";
    } else {
        selector = "#nbgsForm";
    }
    var url = $.webPath() + '/organ/gsjg/save_gsjg?type=' + type;
    $(selector).form('submit', {
        url:url,
        onSubmit:function () {
            var valid = $(this).form('validate');
            if(!valid)
                $(gsjgWin).mask('hide');
            return valid;
        },
        success:function (data) {
            $(gsjgWin).mask('hide');
            var obj = eval('(' + data + ')');
            if (obj.statusCode == '200') {
                $(gsjgWin).window("close");
                $('#gsjg_grid').datagrid('reload');
                $("#west_gsjg_tree").tree("reload");
                $("#parent_gsjg").combotree("reload", $.webPath() + "/organ/gsjg/allgsjg_tree");
                $.messager.show({
                    title:'提示框',
                    msg:obj.message,
                    timeout:3000,
                    showType:'slide'
                });
            } else {
                $.messager.alert('提示', obj.message, 'error');
            }
        }
    });
}
//查询
function gsjg_search() {
    var type = $("#gsjg_toolbar input[name='gsjg_query_type']:checked").val();
    var parentId = $("#parent_gsjg").combotree("getValue");
    var id = $("#_gsjg").combobox("getValue");
    var parentMc = $("#parent_gsjg").combotree("getText");
    var mc = $("#_gsjg").combobox("getText");
    if (!/^[0-9]*[1-9][0-9]*$/.test(id)) { //正整数判断
        id = "";
    }
    var options = $.extend(getGsjgGridOption(type),
        {queryParams:{type:type, parentId:parentId, gsjgId:id, parentMc:parentMc, mc:mc}});
    $('#gsjg_grid').datagrid(options);
}
//根据不同的type得到不同的表头
function getGsjgGridOption(type) {
    var options = {
        url:$.webPath() + "/organ/gsjg/gsjgList",
        toolbar:"#gsjg_toolbar",
        fitColumns:true,
        singleSelect:true,
        pageNumber: 1,
        pagination:true,
        fit:true,
        rownumbers:true,
        onLoadSuccess:function (target) {
            $("#gsjg_del").hide();
            $("#gsjg_mod").hide();
            $("#bmfwsz").hide();
            $("#ckbmfw").hide();
            $("#gsjg_start").hide();
        },
        onClickRow:function (rowIndex, rowData) {
            $("#gsjg_mod").show();
            $("#bmfwsz").show();
            $("#ckbmfw").show();
            if (rowData.sfzx == null || rowData.sfzx == '是') {
                $("#gsjg_del").show();
                $("#gsjg_start").text('启用');
                $("#gsjg_start").removeClass('stop').addClass('start');
                $('#gsjg_start').unbind('click');
                $('#gsjg_start').bind('click', function () {
                    changeGsjgState($.webPath() + '/organ/gsjg/start_gsjg', '确认启用此机构？');
                });
            } else {
                $("#gsjg_del").hide();
//                $("#gsjg_mod").hide();
                $("#gsjg_start").text('停用');
                $("#gsjg_start").removeClass('start').addClass('stop');
                $('#gsjg_start').unbind('click');
                $('#gsjg_start').bind('click', function () {
                    changeGsjgState($.webPath() + '/organ/gsjg/stop_gsjg', '确认停用此机构？');
                });
            }
            $("#gsjg_start").show();
        },
        onDblClickCell:function (rowIndex, field, value) {
//            var rowData = $("#gsjg_grid").datagrid("getSelected");
            if (field == "qc")
                gsjg_modify();
        },
        rowStyler:function(index,row){
            if(row.sfzx == '是') {
                return 'background-color:rgba(255, 130, 87, 0.64);';
            }
        }
    };
    if (type == 0) {
        var columns = {columns:[
            [
                {field:'qc', title:'全称', width:50},
                {field:'jc', title:'简称', width:40},
                {field:'sjjg_jc', title:'上级机构', width:40},
                {field:'bmjl_xm', title:'部门经理', width:30},
                {field:'rcgl_xm', title:'日常管理', width:30},
                {field:'bmzz', title:'部门职责', width:50},
                {field:'sfjyzz', title:'经营机构', align:'center', width:21},
                {field:'sfzjzy', title:'资金占用', align:'center', width:21},
                {field:'khbm_jc', title:'考核部门', width:40},
                {field:'sfzx', title:'启用', align:'center', width:10, formatter:function (value, row, index) {
                    if (value == '是')
                        return '否';
                    else
                        return '是';
                }},
                {field:'bz', title:'备注', width:70}
            ]
        ]}
        options = $.extend(options, columns);
    } else if (type == 1) {
        var columns = {columns:[
            [
                {field:'qc', title:'全称', width:60},
                {field:'jc', title:'简称', width:30},
                {field:'sjjg_jc', title:'上级机构', width:30},
                {field:'zpme', title:'增值票额', width:20},
                {field:'qyfr_qyqc', title:'企业法人', width:50},
                {field:'bgdz', title:'办公地址', width:50},
                {field:'sfzx', title:'启用', align:'center', width:10, formatter:function (value, row, index) {
                    if (value == '是')
                        return '否';
                    else
                        return '是';
                }},
                {field:'bz', title:'备注', width:80}

            ]
        ]}
        options = $.extend(options, columns);
    } else {
        var columns = {columns:[
            [
                {field:'qc', title:'全称', width:50},
                {field:'jc', title:'简称', width:30},
                {field:'sjjg_jc', title:'上级机构', width:30},
                {field:'sfzx', title:'启用', align:'center', width:10, formatter:function (value, row, index) {
                    if (value == '是')
                        return '否';
                    else
                        return '是';
                }},
                {field:'bz', title:'备注', width:100}
            ]
        ]}
        options = $.extend(options, columns);
    }
    return options;
}
//改变状态，启用停用
function changeGsjgState(url, message) {
    var gsjgId = $("#gsjg_grid").datagrid("getSelected").id;
    if (gsjgId) {
        $.messager.confirm("确认", message, function (r) {
            if (r) {
                $.ajax({
                    url:url + "?gsjgId=" + gsjgId,
                    type:"json",
                    success:function (data) {
                        if (data.statusCode == '200') {
                            $('#gsjg_grid').datagrid('reload');
                            $("#west_gsjg_tree").tree("reload");
                            $("#parent_gsjg").combotree("reload", $.webPath() + "/organ/gsjg/allgsjg_tree");
                            $.messager.show({
                                title:'提示框',
                                msg:data.message,
                                timeout:3000,
                                showType:'slide'
                            });
                        } else {
                            $.messager.alert('提示', data.message, 'info');
                        }
                    }
                });
            }
        })
    }
}
